import { AdminInfoState } from '@/store/useAdminInfo';
import api from './index';
import { CourseList, Comment } from './api';
interface ResponseData<T> {
  code: number;
  message: string;
  data: T;
}

export interface ServerPerformanceData {
  cpu: number[]; // CPU数量
  memory: number; // 内存使用率
  userCount: number; // 用户数量
  courseCount: number; // 课程数量
  courseLatestUpdate: CourseList[]; // 课程最新更新
}

export interface UserList {
  id: string;
  username: string;
  phone: string;
  avatar: string;
  created_at: string;
  updated_at: string;
}

interface CourseData {
  id: string,
  name: string,
  description: string,
  image: string,
  type: string;
  category: string;
  content: string;
  video_url: string;
  created_at: string;
  updated_at: string;
}

interface updateCourseData {
  id: string;
  name: string;
  description: string;
  image: string;
  type: string;
  category: string;
  content: string;
  video_url: string;
}

interface addCourseData {
  name: string,
  description: string,
  image: string,
  type: string,
  category: string,
  content: string,
  video_url: string
}


/* 管理员API */
// 登录
export const loginAdminApi = (data: { phone: string; password: string }): Promise<ResponseData<AdminInfoState>> => api.post('/admin/login', data);

// 获取服务器性能数据
export const getServerPerformanceDataApi = (): Promise<ResponseData<ServerPerformanceData>> => api.get('/admin/serverPerformance');

// 获取用户列表
export const getUserListApi = (): Promise<ResponseData<UserList>> => api.get('/admin/userList');

// 新增普通用户
export const addUserApi = (data: { username: string; password: string; phone: string }): Promise<ResponseData<string>> => api.post('/admin/addUser', data);

// 删除普通用户
export const deleteUserApi = (id: string): Promise<ResponseData<string>> => api.delete(`/admin/deleteUser/${id}`);

// 获取课程列表
export const getCourseListApi = (): Promise<ResponseData<CourseData[]>> => api.get('/admin/courseList');

// 新增课程
export const addCourseApi = (data: addCourseData): Promise<ResponseData<string>> => api.post('/admin/addCourse', data);

// 更新课程
export const updateCourseApi = (data: updateCourseData): Promise<ResponseData<string>> => api.post('/admin/updateCourse', data);

// 删除课程
export const deleteCourseApi = (id: string): Promise<ResponseData<string>> => api.get(`/admin/deleteCourse/${id}`);

// 上传文件
export const uploadFileApi = (data: FormData): Promise<ResponseData<string>> => api.post('/upload/img', data);

// 查询二级分类构建key value
export const getCourseCategoryApi = (): Promise<ResponseData<{ key: string, value: string, label: string }[]>> => api.get(`/admin/categoryList`);

// 删除评论
export const deleteCommentApi = (id: string): Promise<ResponseData<string>> => api.delete(`/admin/deleteComment/${id}`);

// 获取评论列表
export const getCommentListApi = (): Promise<ResponseData<Comment[]>> => api.get(`/admin/commentList`);
